package com.zengqingfa.juc.n3;

import lombok.extern.slf4j.Slf4j;

/**
 * @fileName: TestThreadState
 * @author: zengqf3
 * @date: 2021-6-8 20:35
 * @description:
 */
@Slf4j(topic = "c.TestThreadState")
public class TestThreadState {


    /**
     *
     23:21:47:976 [main] c.TestThreadState - t1线程状态：RUNNABLE
     23:21:47:994 [main] c.TestThreadState - t2线程状态：BLOCKED
     * @param args
     * @throws InterruptedException
     */
    public static void main(String[] args) throws InterruptedException {
        Thread t1 = new Thread(() -> {
            synchronized (TestThreadState.class) {
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            synchronized (TestThreadState.class) {
                try {
                    Thread.sleep(1000000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }, "t2");
        t2.start();
        log.debug("t1线程状态：{}", t1.getState());
        log.debug("t2线程状态：{}", t2.getState());
    }

    /**
     * RUNNABLE->WAITING->TERMINATED
     * 23:16:26:226 [main] c.TestThreadState - t1:RUNNABLE
     * 23:16:26:226 [t2] c.TestThreadState - t2线程状态:RUNNABLE
     * 23:16:26:226 [main] c.TestThreadState - t2:RUNNABLE
     * 23:16:28:228 [main] c.TestThreadState - t1:RUNNABLE
     * 23:16:28:228 [t1] c.TestThreadState - 被打断
     * 23:16:28:228 [main] c.TestThreadState - t2:WAITING
     * 23:16:30:243 [main] c.TestThreadState - t1:TERMINATED
     * 23:16:30:243 [main] c.TestThreadState - t2:TERMINATED
     *
     * @throws InterruptedException
     */
    private static void method5() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            while (true) {
                Thread thread = Thread.currentThread();
                if (thread.isInterrupted()) {
                    log.debug("被打断");
                    break;
                }
            }
        }, "t1");
        t1.start();
        Thread t2 = new Thread(() -> {
            try {
                log.debug("t2线程状态:{}", Thread.currentThread().getState());
                t1.join();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t2");
        t2.start();

        log.debug("t1:{}", t1.getState());
        log.debug("t2:{}", t2.getState());

        Thread.sleep(2000);
        t1.interrupt();
        log.debug("t1:{}", t1.getState());
        log.debug("t2:{}", t2.getState());
        Thread.sleep(2000);
        log.debug("t1:{}", t1.getState());
        log.debug("t2:{}", t2.getState());
    }

    /**
     * RUNNABLE->TIMED_WAITING 调用wait方法
     * 20:52:06:608 [main] c.TestThreadState - 线程状态：RUNNABLE
     * 20:52:06:711 [main] c.TestThreadState - 线程状态：TIMED_WAITING
     *
     * @throws InterruptedException
     */
    private static void method4() throws InterruptedException {
        Object object = new Object();
        Thread t1 = new Thread(() -> {
            try {
                synchronized (object) {
                    object.wait(2000);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
        log.debug("线程状态：{}", t1.getState());
        Thread.sleep(100);
        log.debug("线程状态：{}", t1.getState());
    }

    /**
     * RUNNABLE->TERMINATED  主线程调用join方法等待线程结束
     * 20:47:17:143 [main] c.TestThreadState - 线程状态：RUNNABLE
     * 20:47:18:142 [main] c.TestThreadState - 线程状态：TERMINATED
     *
     * @throws InterruptedException
     */
    private static void method3() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
        log.debug("线程状态：{}", t1.getState());
        t1.join();
        log.debug("线程状态：{}", t1.getState());
    }

    /**
     * RUNNABLE->TIMED_WAITING 调用sleep方法
     * 20:45:32:648 [main] c.TestThreadState - 线程状态：RUNNABLE
     * 20:45:32:752 [main] c.TestThreadState - 线程状态：TIMED_WAITING
     *
     * @throws InterruptedException
     */
    private static void method2() throws InterruptedException {
        Thread t1 = new Thread(() -> {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }, "t1");
        t1.start();
        log.debug("线程状态：{}", t1.getState());
        Thread.sleep(100);
        log.debug("线程状态：{}", t1.getState());
    }

    /**
     * NEW->RUNNABLE
     * 20:38:00:633 [main] c.TestThreadState - 线程状态：NEW
     * 20:38:00:636 [main] c.TestThreadState - 线程状态：RUNNABLE
     * 20:38:00:636 [t1] c.TestThreadState - runing....
     */
    private static void method1() {
        Thread t1 = new Thread(() -> {
            log.debug("runing....");
        }, "t1");
        log.debug("线程状态：{}", t1.getState());
        t1.start();
        log.debug("线程状态：{}", t1.getState());
    }
}
